﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HgReleaseNotesGen
{
    using System.IO;

    class Program
    {
        static void Main(string[] args)
        {
            //arg[0] = Change History generated with hg log >c:\changehistory.txt
            //arg[1] = ReleaseHistory file (manually created file)
            //arg[2] = path to release notes file to generated
            if (args.Length != 3)
            {
                Console.WriteLine("Incorrect number of arguments.");
                return;
            }

            if (!File.Exists(args[0].Trim()) || !File.Exists(args[1].Trim()))
            {
                Console.WriteLine("Input files do not exist.");
                return;
            }

            if (File.Exists(args[2].Trim()))
            {
                Console.WriteLine("Output file already exists.");
                return;
            }

            List<Changeset> changesets;

            List<Release> releases;

            var streamReader = File.OpenText(args[0].Trim()); // @"C:\changehistory.txt"
            using (streamReader)
            {
               changesets = Changeset.LoadChangesetsFromStream(streamReader);
            }

            streamReader = File.OpenText(args[1].Trim()); //@"C:\releasehistory.txt"
            using (streamReader)
            {
                releases = Release.LoadReleasesFromStream(streamReader);
            }

            var outputFolder = Path.GetDirectoryName(args[2].Trim());

            Directory.CreateDirectory(outputFolder);

            var streamWriter = new StreamWriter(args[2].Trim()); //@"c:\releasenotes.txt"

            using (streamWriter)
            {
                bool started = false;
                int releasesIndex = 0;
                foreach (var changeset in changesets)
                {
                    if (releasesIndex < releases.Count && changeset.Id.RepositoryId == releases[releasesIndex].ChangesetId.RepositoryId)
                    {
                        if (started)
                        {
                            streamWriter.WriteLine();
                            streamWriter.WriteLine();
                        }
                        else
                        {
                            started = true;
                        }

                        var release = releases[releasesIndex];
                        streamWriter.WriteLine("{0} ({1})", release.VersionNumber, release.ChangesetId.RepositoryId);
                        streamWriter.WriteLine("=======================");
                        foreach (var e in release.Notes)
                        {
                            streamWriter.WriteLine(e);
                        }

                        releasesIndex++;
                    }
                    if (started)
                    {
                        streamWriter.WriteLine(changeset.Summary);
                    }
                }
            }

            Console.WriteLine("Release Notes generated at {0}", args[2].Trim());
        }
    }
}
